import { PAGENAME } from './PageName';

export const DOMAIN_URL = 'http://121.37.208.83:8888/';
const BASE_URL = `${DOMAIN_URL}/api/v1`;

const api = async (endpoint: string, method = 'POST', data: any = null) => {
  const url = `${BASE_URL}${endpoint}`;

  // 添加会话管理逻辑，如添加token到请求头
  const headers = {
    // Authorization: `Bearer ${token}`,
    // Cookie: token,
    'Content-Type': 'application/json',
  };

  const options = {
    method,
    headers,
    credentials: 'include',
    body: data ? JSON.stringify(data) : null,
  };

  try {
    console.log(url, data, options);
    const response = await fetch(url, options);
    if (!response.ok) {
      if (response.status == 401) {
        console.log(response.status);
        return { code: 401, msg: '请注销后重新登录' };
      }
      throw new Error(
        `Network response was not ok, ${url} ${response.status}  ${response.statusText}`,
      );
    }
    console.log(url, response);

    return await response.json();
  } catch (error) {
    console.error('Error:', error);
    throw error;
  }
};
const apiForm = async (endpoint: string, method = 'POST', data: any = null) => {
  const url = `${BASE_URL}${endpoint}`;

  // 添加会话管理逻辑，如添加token到请求头
  const headers = {
    // Authorization: `Bearer ${token}`,
    // Cookie: token,
    'Content-Type': 'multipart/form-data',
  };

  const options = {
    method,
    headers,
    credentials: 'include',
    body: data,
    // body: data ? JSON.stringify(data) : null,
  };

  try {
    // @ts-ignore
    console.log(url, data, options);
    const response = await fetch(url, options);
    console.log(url, response);
    if (!response.ok) {
      if (response.status == 401) {
        console.log(response.status);
        return { code: 401, msg: '请注销后重新登录' };
      }
      throw new Error(
        `Network response was not ok, ${url} ${response.status}  ${response.statusText}`,
      );
    }
    return await response.json();
  } catch (error) {
    console.error('Error:', error);
    throw error;
  }
};

export const loginUser = async (user: any, password: any, admin: any) => {
  return api(`/users/login`, 'POST', {
    username: user,
    password: password,
    rememberMe: true,
    isAdmin: admin,
  });
};
export const addEvent = async (data: any) => {
  return api('/event/add', 'POST', data);
};
export const uploadFile = async (
  file: { uri: string; type: string; name: string },
  fileType: string,
) => {
  const formData = new FormData();
  formData.append('file', file);
  formData.append('attachmentInfo', `{"type":"${fileType}"}`);
  console.log('upload file', file);
  console.log('upload data', formData);
  return apiForm('/attachments/upload', 'POST', formData);
};
export const userLogin = async (data: any) => {
  return api('/user/login', 'POST', data);
};
export const userRank = async (data: any) => {
  return api('/user/rank', 'POST', data);
};
export const userInfoGet = async (data: any) => {
  return api('/user/info/get', 'POST', data);
};
export const userInfoUpdate = async (data: any) => {
  return api('/user/info/update', 'POST', data);
};
export const userScoreRecent = async (data: any) => {
  return api('/user/score/recent', 'POST', data);
};
export const usersList = async (data: any) => {
  return api('/users/list', 'POST', data);
};
export const usersUpdate = async (data: any) => {
  return api('/users/update', 'POST', data);
};
export const usersAdd = async (data: any) => {
  return api('/users/add', 'POST', data);
};
export const usersDelete = async (data: any) => {
  return api('/users/delete', 'POST', data);
};
export const eventRecent = async (data: any) => {
  return api('/event/recent', 'POST', data);
};
export const newsRecent = async (data: any) => {
  return api('/news/recent?count=3', 'POST', data);
};
export const studyRecent = async (data: any) => {
  return api('/study/recent', 'POST', data);
};
export const deptInfoGet = async () => {
  return api('/dept/info/get', 'POST', null);
};
export const deptInfoUpdate = async (data: any) => {
  return api('/dept/info/update', 'POST', data);
};
export const scoreRank = async (data: any) => {
  return api('/score/rank', 'POST', data);
};
export const honorList = async (data: any) => {
  console.log('url ====' + `/honor/list?pageNum=${data.pageNum}&pageSize=${data.pageSize}&orderByColumn=applydate&isAsc=desc`)
  return api(`/honor/list?pageNum=${data.pageNum}&pageSize=${data.pageSize}&orderByColumn=applydate&isAsc=desc`, 'POST', null);
};
export const honorDetail = async (data: any) => {
  return api(`/honor/detail?honorID=${data}`, 'POST', data);
};
export const honorReview = async (data: any) => {
  return api(`/honor/review?ids=${data.ids}&status=${data.status}`, 'POST', data);
};
export const honorAdd = async (data: any) => {
  return api('/honor/add', 'POST', data);
};
export const noticeUnread = async (data: any) => {
  return api('/notice/unread', 'POST', data);
};
export const noticeDetail = async (data: any) => {
  return api(`/notice/detail?noticeID=${data}`, 'POST', data);
};
export const noticeList = async (data: any) => {
  return api('/notice/list?pageNum=1&pageSize=99999999', 'POST', data);
};
export const userUnread = async (data: any) => {
  return api(`/notice/user/unread?projectID=${data}`, 'POST', null);
};
export const userRead = async (data: any) => {
  return api(`/notice/user/read?projectID=${data}`, 'POST', null);
};
export const noticeUpdate = async (data: any) => {
  return api('/notice/update', 'POST', data);
};
export const noticeAdd = async (data: any) => {
  return api('/notice/add', 'POST', data);
};
export const noticeDelete = async (data: any) => {
  return api(`/notice/delete?ids=${data}`, 'POST', data);
};
export const eventList = async (page, size) => {
  const data = {
    pageNum: page,
    pageSize: size,
  };
  return api('/event/list', 'POST', data);
};
export const eventDetail = async (id: any) => {
  return api(`/event/detail?id=${id}`, 'POST');
};
export const eventUpdate = async (data: any) => {
  return api('/event/update', 'POST', data);
};
export const eventAdd = async (data: any) => {
  return api('/event/add', 'POST', data);
};
export const eventDelete = async (id: any) => {
  return api(`/event/delete?id=${id}`, 'POST');
};
export const eventApplyAdd = async (data: any) => {
  return api('/event/apply/add', 'POST', data);
};
export const eventApplyList = async (id, page, size) => {
  return api(`/event/apply/list?activityId=${id}`, 'POST', {
    pageNum: page,
    pageSize: size,
  });
};
export const eventApplyDetail = async (data: any) => {
  return api('/event/apply/detail', 'POST', data);
};
export const eventApplyReview = async (id: string, status: string) => {
  return api(
    `/event/apply/review?approvalId=${id}&status=${status}`,
    'POST',
    null,
  );
};
export const noticeEventAdd = async (data: any) => {
  return api('/notice/event/add', 'POST', data);
};
export const lessonList = async (page, size) => {
  const data = {
    pageNum: page,
    pageSize: size,
  };
  return api('/lesson/list', 'POST', data);
};
export const lessonDetail = async (data: any) => {
  return api(`/lesson/detail?id=${data}`, 'POST');
};
// export const lessonUnjoin = async (data: any) => {
//   return api('/lesson/unjoin', 'POST', data);
// };
// export const lessonJoin = async (data: any) => {
//   return api('/lesson/join', 'POST', data);
// };
export const lessonUpdate = async (data: any) => {
  return api('/lesson/update', 'POST', data);
};
export const lessonAdd = async (data: any) => {
  return api('/lesson/add', 'POST', data);
};
export const lessonScoreAdd = async (projectId: any) => {
  return api(`/score/add?projectId=${projectId}`, 'POST');
};
export const lessonDelete = async (data: any) => {
  return api(`/lesson/delete?id=${data}`, 'POST', data);
};
export const studyDelete = async (data: any) => {
  return api(`/study/delete?id=${data}`, 'POST', data);
};

export const studyList = async (page, size) => {
  const data = {
    pageNum: page,
    pageSize: size,
  };
  return api('/study/list', 'POST', data);
};
export const studyDetail = async (data: any) => {
  return api(`/study/detail?id=${data}`, 'POST', data);
};
// export const studyUnjoin = async (data: any) => {
//   return api('/study/unjoin', 'POST', data);
// };
// export const studyJoin = async (data: any) => {
//   return api('/study/join', 'POST', data);
// };
export const studyUpdate = async (data: any) => {
  return api('/study/update', 'POST', data);
};
export const studyAdd = async (data: any) => {
  return api('/study/add', 'POST', data);
};
export const newsList = async (data: any) => {
  return api('/news/list?pageNum=1&pageSize=99999999', 'POST', data);
};
export const newsDetail = async (data: any) => {
  return api(`/news/detail?newsID=${data}`, 'POST', data);
};
export const newsUpdate = async (data: any) => {
  return api('/news/update', 'POST', data);
};
export const newsAdd = async (data: any) => {
  return api('/news/add', 'POST', data);
};
export const newsDelete = async (data: any) => {
  return api(`/news/delete?ids=${data}`, 'POST', data);
};

//我的
export const getCurrentUserInfo = async (data: any) => {
  return api('/users/info/get', 'POST', data);
};
export const getUserInfoByLoginName = async (data: any) => {
  return api(`/users/view?loginName=${data}`, 'POST', data);
};
export const getTotalPointsPs = async (data: any) => {
  return api(`/score/getpersonalpoints?userId=${data}`, 'POST', null);
};
export const getMouthPointsPs = async (data: any) => {
  return api(`/score/getpersonalmonthpoints?userId=${data}`, 'POST', null);
};
export const getTotalPointsRankPs = async (data: any) => {
  return api(`/score/rank`, 'POST', null);
};
export const getMouthPointsRankPs = async (data: any) => {
  return api(`/score/monthrank`, 'POST', null);
};
export const getGroupPointsRankPs = async (data: any) => {
  return api(`/score/grouprank`, 'POST', null);
};

//用户管理
export const editUserInfo = async (data: any) => {
  return api(`/users/update`, 'POST', data);
};
export const addUserInfo = async (data: any) => {
  return api(`/users/add`, 'POST', data);
};
export const getUserInfoList = async (data: any) => {
  return api('/users/list?pageNum=1&pageSize=99999999', 'POST', data);
};
export const deleteUserInfoById = async (data: any) => {
  return api(`/users/delete?ids=${data}`, 'POST', data);
};
export const getGroupList = async (data: any) => {
  return api('/users/groupList', 'POST', data);
};
export const getDeptList = async (data: any) => {
  return api('/users/deptList', 'POST', data);
};

//支部信息
export const companyEdit = async (data: any) => {
  return api('/dept/info/update', 'POST', data);
};
